<html>
<head><title>Reset</title>
<link rel="stylesheet" type="text/css" href="css/Default.css" />
<Script language="javascript" src="data/password.js"></script>
<Script language="javascript" src="exec/login.js"></script>
<noscript>
location.href="noscript.html";
</noscript>
<SCRIPT LANGUAGE="JavaScript">

	window.onload = redir(0);
	
//name of cookie: 'eomdmf'

</SCRIPT>
<?php
//here we check for reset password + username.

//Import form information
$cookie = $_COOKIE["eomdmf"]; //Gets cookie by name of eomdmf
$user = $_POST["User"];
$pass = $_POST["Pass"];
$code = $_POST["Code"];
include './exec/GetRoot.php'; //Contains root password and username, for checking against
$auth = false;
//since anyone that isn't on the server can't see php code, we use a php file to compare ROOT username + password
if($user == $root_user && $pass == $root_pass)
{	
	//ensure that they logged in. and JS isn't turned off
	if($cookie == $root_user.":".$root_pass)
	{
		//make sure the temp codes match.
		$fh = fopen("tmp.txt", "r");		$temp =trim(fgets($fh));		fclose($fh);
		if($code==$temp)
		{
			//ok they are authorised to do this.
			$auth=true;
		}
	}
}
else if($user != "")
{
	echo "<script language='javascript'>alert('Wrong Username/Password.');</script>";
}
	//wipe temp code.
	if( file_exists("tmp.txt") && unlink("tmp.txt") ==false)
	{
		echo "Couldn't delete tmp.txt";
	}

if($auth)
{
	//delete all .js files that contain data that is read by the main page. keep .txt files. DON"T touch media player stuff. If that needs to be reset then the tv's need to be shut down.
	$del = array("data/announce_data.js","data/newsfeed_data.js");
	//use loop incase more files need to be added later.
	for($i=0; $i<sizeof($del); $i++){
		if( file_exists($del[$i]) && unlink($del[$i]) == false){
			echo "couldn't delete ".$del[$i]."\n";
			continue;
		}
	}
	echo "<script language='javascript'>alert('Deletion of files succesful.');location.href='LoggedIn.php';</script>";
	//die();
	//stop here, don't load the rest of the page.
}
?>

</HEAD>
<body>
<fieldset style="border-color:bfae20">
	<font size="3.5" face="Arial">
	<b>
	Digital Media Feed
	</b>
	</font>
<center>
<table border="0">
<tr>
<td>
Are you sure you want to reset? (Please enter your password and username below)
<br><br>

<form method="post" action="Reset.php" name="login">
<center>Username <input type="text" id="User" name="User" size="20"></center>
<br>
<center>Password <input type="password" id="Pass" name="Pass" size="20"></center>
<br>
<center>
Please enter the following in the box below:
<?php
	//The random number is to ensure the user really wants to reset
	
	$var = mt_Rand(); //Generate random number ( Range: 0 - mt_getrandmax() )
	$fh = fopen("tmp.txt", "w"); //Writes random number to file
	if($fh==false){
		echo "Couldn't write to tmp.txt";
	}	fwrite($fh,$var."");
	fclose($fh);
echo $var; //Displays random number, so user can see
?>
<br>
<input type="text" id="Code" name="Code" size="18"><br>
<INPUT TYPE="submit" id="subbut" VALUE="Confirm Reset">
</center>
</form>
<br>
<center>
<FORM METHOD="LINK" ACTION="LoggedIn.php">
<INPUT TYPE="submit" VALUE="Return to Main Menu">
</FORM>

<FORM METHOD="LINK" ACTION="index.html">
<INPUT TYPE="submit" VALUE="Home Page">
</FORM>
</center>

</center>	
</td>
</tr>
</table>
</center>
</fieldset>

<center>
<img src= "media/Gold_Lion.jpg"/>
</center>

</body>
</html>
